package com.zhny.aiot.led.four.screen.plugin.device;

import com.zhny.aiot.led.four.screen.plugin.config.LedConfig;
import com.zhny.aiot.led.four.screen.plugin.factory.ILedCmdFactory;
import com.zhny.aiot.led.four.screen.plugin.factory.imp.LedCloseCmdFactoryImpl;
import com.zhny.aiot.led.four.screen.plugin.factory.imp.LedModifyCmdFactoryImpl;
import com.zhny.aiot.led.four.screen.plugin.factory.imp.LedOpenCmdFactoryImpl;
import com.zhny.aiot.led.four.screen.plugin.model.LedCommand;
import com.zhny.aiot.led.four.screen.plugin.model.LedStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.utils.MessageUtils;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.plugin.udc.UdcEngine;
import com.zhny.aiot.plugin.udc.command.IUdcPrivateRequestMessage;
import com.zhny.aiot.plugin.udc.device.UdcDevice;
import io.netty.buffer.ByteBuf;
import org.pf4j.util.StringUtils;

import java.io.UnsupportedEncodingException;

public class LedDevice extends UdcDevice<LedConfig> {
    private final ILedCmdFactory ledCmdFactory;
    public LedDevice(UdcEngine engine, IDeviceFactory<LedConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
        ledCmdFactory =
                new LedModifyCmdFactoryImpl(getConfig(),
                        new LedOpenCmdFactoryImpl(getConfig(),
                                new LedCloseCmdFactoryImpl(getConfig(),null)));
    }

    @Override
    protected IUdcPrivateRequestMessage createRequestMessage(TaskEntity taskEntity) {
        if(!StringUtils.isNullOrEmpty(taskEntity.getMethodName())){
            try {
                LedCommand command =ledCmdFactory.handler(taskEntity.getMethodName(),taskEntity.getParams());
                return command;
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage(),e);
                return null;
            }
        }
        return null;
    }

    @Override
    protected void onData(ByteBuf byteBuf) {
        logger.info("led start decode !");
        int headHig = byteBuf.readByte();
        int headLow = byteBuf.readByte();
        int address = byteBuf.readShortLE();
        int cmd = byteBuf.readByte();
        byteBuf.readBytes(4);
        byteBuf.readBytes(4);
        int len = byteBuf.readUnsignedShort();
        int data =byteBuf.readUnsignedShort();
        int crc = byteBuf.readUnsignedShort();
        int last = byteBuf.readByte();
        onMessage(cmd,data);
        logger.info("led end decode !");
    }

    private void onMessage(int cmd, int data) {
        LedStoreEntry entry = new LedStoreEntry();
        String status=null;
        if(cmd == 0x22)
            status =data== 0 ?"关机":"错误";
        else if(cmd == 0x21)
            status =data== 0 ?"开机":"错误";
        else if(cmd == 0x29)
            status = data == 0?"设置成功":"设置错误";
        entry.setStatus(status);
        MessageEntry resultMsg = MessageUtils.createMessageEntry (this.getId ());
        resultMsg.setType(MessageType.RESULT);
        resultMsg.setData(entry);
        nofity(resultMsg);
        saveReal(entry);
    }

    @Override
    public boolean onCheck(ByteBuf byteBuf) {
        int len = byteBuf.readableBytes();
        int headHig = byteBuf.readByte() & 0xff;
        int headLow = byteBuf.readByte() & 0xff;
        if(headHig == 121 && headLow == 52)
            if(len == 20)
                return true;
        return false;
    }

    @Override
    protected void disposable() {

    }
}
